import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/widges/appbar.dart';
import 'package:guochaoapp/controller/shopping/shopping_settle_controller.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';
import '../../r.dart';

class GCShoppingSettlePage extends StatelessWidget {
  final c = Get.put(GCShoppingSettleController());

  @override
  Widget build(BuildContext context) {
    if(Get.arguments.data != null)
     c.settleData = Get.arguments.data;
    c.requestInitData();
    return Scaffold(
        appBar: GCAppBar(titleText: "确认订单"), body: Obx(() => _initSubviews()));
  }

  _initSubviews() {
    return Column(children: [
      _getEmptyAddressView(),
      _getDefaultAddressView(),
      _getSettleView(),
      Expanded(child: SizedBox()),
      _getSubmitView(),
    ]);
  }

  _getDefaultAddressView() {
    return Offstage(
        offstage: c.address.value.addrId == null,
        child: Container(
          color: Colors.white,
          // height: ScreenUtil().setSp(83),
          child: Column(
            children: [
              SizedBox(height: ScreenUtil().setSp(8)),
              Row(
                children: [
                  SizedBox(width: ScreenUtil().setSp(21)),
                  Text(
                    "收货地址",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(14),
                        fontWeight: FontWeight.bold),
                  ),
                  Expanded(child: SizedBox()),
                  GestureDetector(
                      onTap: () {
                        c.chooseAddress();
                      },
                      child: Image.asset(R.assetsIconArrowRight,
                          width: 20, height: 20)),
                  SizedBox(width: ScreenUtil().setSp(10)),
                ],
              ),
              SizedBox(height: ScreenUtil().setSp(11)),
              Row(
                children: [
                  SizedBox(width: ScreenUtil().setSp(21)),
                  Text(
                    "联系人: ${c.address.value.receiverName}",
                    style: TextStyle(fontSize: ScreenUtil().setSp(14)),
                  ),
                  Expanded(child: SizedBox()),
                  Text(
                    "${c.address.value.receiverPhone}",
                    style: TextStyle(fontSize: ScreenUtil().setSp(14)),
                  ),
                  SizedBox(width: ScreenUtil().setSp(25)),
                ],
              ),
              SizedBox(height: ScreenUtil().setSp(11)),
              Row(
                children: [
                  SizedBox(width: ScreenUtil().setSp(20)),
                  Image.asset(R.assetsIconSettleAddress,
                      height: ScreenUtil().setSp(26),
                      width: ScreenUtil().setSp(14)),
                  SizedBox(width: ScreenUtil().setSp(2)),
                  Text(
                    "${c.address.value.receiverAddr}",
                    style: TextStyle(fontSize: ScreenUtil().setSp(14)),
                  )
                ],
              ),
              SizedBox(height: ScreenUtil().setSp(14)),
              Container(
                height: ScreenUtil().setSp(4),
                width: ScreenUtil().screenWidth,
                child: Image.asset(R.assetsIconSettleDividingLine,
                    fit: BoxFit.cover),
              )
            ],
          ),
        ));
  }

  _getEmptyAddressView() {
    return Offstage(
        offstage: c.address.value.addrId != null,
        child: Container(
          color: Colors.white,
          // height: ScreenUtil().setSp(83),
          child: Column(
            children: [
              SizedBox(height: ScreenUtil().setSp(8)),
              Row(
                children: [
                  SizedBox(width: ScreenUtil().setSp(21)),
                  Text(
                    "收货地址",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(14),
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),
              SizedBox(height: ScreenUtil().setSp(11)),
              GestureDetector(
                  onTap: () {
                    c.chooseAddress();
                  },
                  child: Row(children: [
                    SizedBox(width: ScreenUtil().setSp(20)),
                    Container(
                        child: Stack(children: [
                      Image.asset(R.assetsIconRectangle,
                          height: ScreenUtil().setSp(26),
                          width: ScreenUtil().setSp(342),
                          fit: BoxFit.cover),
                      Row(
                        children: [
                          SizedBox(width: ScreenUtil().setSp(7)),
                          Image.asset(R.assetsIconSettleAddress,
                              height: ScreenUtil().setSp(26),
                              width: ScreenUtil().setSp(14)),
                          SizedBox(width: ScreenUtil().setSp(8)),
                          Opacity(
                            opacity: 0.7,
                            child: Text(
                              "请添加收货地址",
                              style: TextStyle(
                                fontSize: ScreenUtil().setSp(12),
                              ),
                            ),
                          )
                        ],
                      )
                    ]))
                  ])),
              SizedBox(height: ScreenUtil().setSp(14)),
              Container(
                height: ScreenUtil().setSp(4),
                width: ScreenUtil().screenWidth,
                child: Image.asset(R.assetsIconSettleDividingLine,
                    fit: BoxFit.cover),
              )
            ],
          ),
        ));
  }

  _getSettleView() {
    return Container(
        color: Colors.white,
        // height: 400,
        width: ScreenUtil().screenWidth,
        margin: EdgeInsets.only(
            left: ScreenUtil().setSp(8),
            right: ScreenUtil().setSp(8),
            top: ScreenUtil().setSp(4)),
        padding: EdgeInsets.only(
            left: ScreenUtil().setSp(20),
            right: ScreenUtil().setSp(18),
            top: ScreenUtil().setSp(12)),
        child: Column(
          children: [
            Row(
              children: [
                Image.asset(R.assetsIconSettleMall,
                    height: ScreenUtil().setSp(16),
                    width: ScreenUtil().setSp(16)),
                SizedBox(width: ScreenUtil().setSp(5)),
                Text("果巢商城",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(14),
                        fontWeight: FontWeight.bold)),
                Expanded(child: SizedBox()),
                Container(
                  width: ScreenUtil().setSp(65),
                  height: ScreenUtil().setSp(26),
                  child: OutlineButton.icon(
                    icon: Image.asset(R.assetsIconSettleList,
                        height: ScreenUtil().setSp(15),
                        width: ScreenUtil().setSp(15)),
                    label: Text("清单"),
                    padding: EdgeInsets.zero,
                    color: Colors.white,
                    textColor: Colors.black54,
                    borderSide: BorderSide(color: Color(0xFF40A8B3)),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(4))),
                    onPressed: () {
                      c.jumpToList(c.settleData.itemList);
                    },
                  ),
                )
              ],
            ),
            SizedBox(height: ScreenUtil().setSp(7)),
            Divider(
              height: ScreenUtil().setSp(1),
              color: Color(0xFFF0F0F0),
            ),
            Container(
              height: ScreenUtil().setSp(33) * c.settleData.itemList.length,
              child: ListView.builder(
                  itemCount: c.settleData.itemList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getListView(index);
                  }),
            ),
            SizedBox(height: ScreenUtil().setSp(5)),
            Row(
              children: [
                Text(
                  "总重量",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "${c.getWeightTotal()}",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setSp(5)),
            Row(
              children: [
                Text(
                  "商品总额",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "￥${c.settleData.priceTotal}",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setSp(5)),
            Row(
              children: [
                Text(
                  "运费",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "+${c.freight.value.freightTotal == null ? 0.0 : c.freight.value.freightTotal}",
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(12), color: Colors.red),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setSp(5)),
            Row(
              children: [
                Text(
                  "余额抵扣",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "-${c.settleData.userBalance}",
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(12), color: Colors.green),
                ),
              ],
            ),
            SizedBox(height: ScreenUtil().setSp(15))
          ],
        ));
  }

  _getListView(int index) {
    CartSettleDataItemList data = c.settleData.itemList[index];
    return Container(
      padding: EdgeInsets.only(top: ScreenUtil().setSp(3)),
      height: ScreenUtil().setSp(33),
      child: Column(
        children: [
          Row(
            children: [
              Container(
                height: ScreenUtil().setSp(26),
                child: FlatButton(
                  child: Row(
                    children: [
                      Image.network(data.categoryImg,
                          height: ScreenUtil().setSp(15),
                          width: ScreenUtil().setSp(15)),
                      SizedBox(width: ScreenUtil().setSp(4)),
                      Text("${data.categoryName}")
                    ],
                  ),
                  color: Color(0xFF40A8B3),
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(4))),
                  onPressed: () {},
                ),
              ),
              SizedBox(width: ScreenUtil().setSp(3)),
              Text("(共${data.itemNum}件)",
                  style: TextStyle(fontSize: ScreenUtil().setSp(12))),
              Expanded(child: SizedBox()),
              Opacity(
                  opacity: 0.5,
                  child: Text("￥${data.itemPrice}",
                      style: TextStyle(fontSize: ScreenUtil().setSp(12)))),
            ],
          ),
          SizedBox(height: ScreenUtil().setSp(3)),
          Divider(
            height: ScreenUtil().setSp(1),
            color: Color(0xFFF0F0F0),
          ),
        ],
      ),
    );
  }

  _getSubmitView() {
    return Container(
      height: ScreenUtil().setSp(64),
      color: Colors.white,
      child: Row(children: [
        SizedBox(width: ScreenUtil().setSp(27)),
        Text(
          "实付金额:",
          style: TextStyle(fontSize: ScreenUtil().setSp(14)),
        ),
        Text(
          "￥${c.getPaymentPrice()}",
          style: TextStyle(fontSize: ScreenUtil().setSp(14), color: Colors.red),
        ),
        Expanded(child: SizedBox()),
        Container(
          height: ScreenUtil().setSp(37),
          width: ScreenUtil().setSp(110),
          margin: EdgeInsets.only(bottom: ScreenUtil().setSp(2)),
          child: RaisedButton(
            child: Text("提交订单"),
            color: Color(0xFF40A8B3),
            textColor: Colors.white,
            onPressed: () {
              c.submitOrder();
            },
          ),
        ),
        SizedBox(width: ScreenUtil().setSp(21)),
      ]),
    );
  }
}
